<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:40
 * 帅帅吴
 */

namespace app\admin\controller;

use app\admin\model\FamilyRoom;
use app\admin\model\GiftRecord;
use app\admin\model\IntroduceUser;
use app\admin\model\PersonalRoom;
use app\admin\model\Room as RoomModel;
use app\admin\model\Family as FamilyModel;
use app\admin\model\RoomMembers;
use app\admin\model\SortWheat;
use app\admin\model\User;
use think\Db;
use think\wyyxapi\WyYxAPI;

class Room extends Index
{
    /**
     * @return \think\response\View
     * 房间流水渲染模板
     * 帅帅吴
     */
    public function roomFlowing(){
        return view();
    }

    /**
     * Notes: 获取房间流水数据
     * User: 王俊成
     * Date: 2018/10/31
     * Time: 11:16
     * @return \think\response\Json
     */
    public function roomFlowingData(){
        $roomModel = new RoomModel();
        $data = $roomModel->roomFlowingData();
        return json($data);
    }

    /**
     * Notes: 某房间流水页
     * User: 王俊成
     * Date: 2018/10/31
     * Time: 15:45
     * @return \think\response\View
     */
    public function roomFlowingInfo(){
        return view()->assign('room_id',input('get.roomId'));
    }
    /**
     * Notes: 获取某个房间流水
     * User: 王俊成
     * Date: 2018/10/31
     * Time: 16:59
     * @return \think\response\Json
     */
    public function roomFlowingInfoData(){
        $roomModel = new RoomModel();
        $data = $roomModel->roomFlowingInfoData();
        return json($data);
    }

    /**
     * Notes: 获取房间某天礼物流水详情
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 10:57
     * @return \think\response\View
     */
    public function roomGiftInfo(){
        $roomModel = new RoomModel();
        $data = $roomModel->roomGiftInfo();
        return view()->assign('data',$data);
    }

    /**
     * Notes: 获取房间某天小时级流水
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 13:23
     * @return $this
     */
    public function roomFlowingHour(){
        $roomModel = new RoomModel();
        $data = $roomModel->roomFlowingHour();
        return view()->assign('data',$data);
    }

    /**
     * Notes: 查看上周排行
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 14:00
     * @return $this
     */
    public function roomFlowingLastweek(){
        $roomModel = new RoomModel();
        $data = $roomModel->roomFlowingLastweek();
        return view()->assign('data',$data);
    }

    /**
     * Notes: 查看房间用户
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 15:26
     * @return $this
     */
    public function getRoomUser(){
        $roomModel = new RoomModel();
        $data = $roomModel->getRoomUser();
        return view('room_user')->assign([
            'data' => $data,
            'room_id' => input('get.roomId')
        ]);
    }

    /**
     * @title 踢出房间
     * @user 帅帅吴
     * @date 2019/2/18
     * @time 14:20
     */
    public function kickUser()
    {
        $roomMemberModel = new RoomMembers();
        $res = $roomMemberModel->kickUser();
        // dump($res);
        return $res;
    }

    //批量踢出房间
    public function kickAllUser()
    {
        $roomMemberModel = new RoomMembers();
        $res = $roomMemberModel->kickAllUser();
        return $res;
    }


    /**
     * @return array
     * @user shuaishuai wu
     * @date 2018/12/13
     * @time 自定义时间求流水和总流水
     */
    public function roomFlowingCount()
    {
        $giftRecord =  new GiftRecord();
        $data = $giftRecord->roomFlowingCount();
        return $data;
    }

    /**
     * @return array|\PDOStatement|string|\think\Collection
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 11:20
     * @title 房间流水排行榜
     */
    public function roomFlowingRank()
    {
        return view();
    }

    /**
     * @return $this
     * @user 帅帅 吴
     * @date 2018/12/24
     * @time 13:34
     * @title 自定义时间排行
     */
    public function roomFlowingRankData()
    {
        $giftRecord =  new GiftRecord();
        $res = $giftRecord->roomFlowingRankData();
        return $res;

    }

    /**
     * @return \think\response\View
     * 家族流水渲染模板
     * 帅帅吴
     */
    public function familyFlowing(){
        return view();
    }

    /**
     * Notes: 家族流水数据
     * User: 王俊成
     * Date: 2018/11/2
     * Time: 9:50
     * @return \think\response\Json
     */
    public function familyFlowingData(){
        $familyModel = new FamilyModel();
        $data = $familyModel->familyFlowingData();
        return json($data);
    }

    /**
     * Notes: 查看家族流水页
     * User: 王俊成
     * Date: 2018/11/2
     * Time: 13:54
     */
    public function familyFlowingInfo(){
        return view()->assign('familyId',input('get.familyId'));
    }

    /**
     * Notes: 获取家族月流水详情
     * User: 王俊成
     * Date: 2018/11/2
     * Time: 14:05
     * @return \think\response\Json
     */
    public function familyFlowingInfoData(){
        $familyModel = new FamilyModel();
        $data = $familyModel->familyFlowingInfoData();
        return json($data);
    }

    /**
     * Notes: 家族天流水详情
     * User: 王俊成
     * Date: 2018/11/2
     * Time: 14:24
     * @return $this
     */
    public function familyFlowingDay(){
        $familyModel = new FamilyModel();
        $data = $familyModel->familyFlowingDay();
        return view()->assign([
            'data' => $data,
            'dates' => input('get.dates')
        ]);
    }

    /**
     * Notes: 家族天流水成员收礼记录数据
     * User: 王俊成
     * Date: 2018/11/3
     * Time: 10:08
     * @return $this
     */
    public function familyFlowingDayDetail(){
        $familyModel = new FamilyModel();
        $data = $familyModel->familyFlowingDayDetail();
        return view()->assign('data',$data);
    }

    /**
     * Notes: 导出上周家族流水
     * User: 王俊成
     * Date: 2018/11/3
     * Time: 13:27
     */
    public function familyFlowingExport(){
        $familyModel = new FamilyModel();
        $familyModel->familyFlowingExport();
    }

    /**
     * @return mixed
     * @user shuaishuai wu
     * @date 2018/12/17
     * @time 17:23
     * @title 计算家族总流水，和自定义流水
     */
    public function familyFlowingCount()
    {
        $giftRecord =  new GiftRecord();
        $data = $giftRecord->familyFlowingCount();
        return $data;
    }

    /**
     * @return \think\response\View
     * @user shuasuhaiwu
     * @date 2018/12/24
     * @time 15:14
     * @title 家族流水排行榜
     */
    public function familyFlowingRank()
    {
        return view();
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 15:16
     * @title 家族流水排行榜数据
     */
    public function familyFlowingRankData()
    {
        $giftRecord =  new GiftRecord();
        $res = $giftRecord->familyFlowingRankData();
        return $res;
    }
    /**
     * @return \think\response\View
     * 推荐人流水
     * 帅帅吴
     */
    public function introduceFlowing(){
        return view();
    }

    /**
     * Notes: 推荐人流水数据
     * User: 王俊成
     * Date: 2018/11/3
     * Time: 17:23
     * @return \think\response\Json
     */
    public function introduceFlowingData(){
        $introduceUserModel = new IntroduceUser();
        $data = $introduceUserModel->introduceFlowingData();
        return json($data);
    }

    /**
     * Notes: 被推荐人员信息
     * User: 王俊成
     * Date: 2018/11/5
     * Time: 10:45
     * @return $this
     */
    public function introduceUserInfo(){
        $introduceUserModel = new IntroduceUser();
        $data = $introduceUserModel->introduceUserInfo();
        return view()->assign('data',$data);
    }

    /**
     * Notes: 家族房间流水页
     * User: 王俊成
     * Date: 2018/11/5
     * Time: 15:54
     * @return \think\response\View
     */
    public function familyRoomFlowing(){
        return view();
    }

    /**
     * Notes: 家族房间流水数据
     * User: 王俊成
     * Date: 2018/11/5
     * Time: 15:56
     * @return \think\response\Json
     */
    public function familyRoomFlowingData(){
        $familyRoomModel = new FamilyRoom();
        $data = $familyRoomModel->familyRoomFlowingData();
        return json($data);
    }

    /**
     * Notes: 家族房间月流水数据
     * User: 王俊成
     * Date: 2018/11/5
     * Time: 17:49
     * @return $this
     */
    public function familyRoomFlowingInfo(){
        return view()->assign('roomId',input('get.roomId'));
    }

    /**
     * Notes: 导出家族房间上周流水信息
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 15:00
     */
    public function familyRoomFlowingExport(){
        $familyRoomModel = new FamilyRoom();
        $familyRoomModel->familyRoomFlowingExport();
    }

    /**
     * @return \think\response\View
     * @user shuaishuai wu
     * @date 2018/12/24
     * @time 17:57
     * @title 家族房间流水排行榜
     */
    public function familyRoomFlowingRank()
    {
        return view();
    }
    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 18:04
     * @title 家族房间流水排行榜
     */
    public function FamilyRoomFlowingRankData()
    {
        $giftRecord =  new GiftRecord();
        $data = $giftRecord->FamilyRoomFlowingRankData();
        return $data;
    }
    /**
     * Notes: 个人房间流水页
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 16:46
     * @return \think\response\View
     */
    public function personalRoomFlowing(){
        return view();
    }

    /**
     * Notes: 个人房间流水数据
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 16:50
     * @return \think\response\Json
     */
    public function personalRoomFlowingData(){
        $personalRoomModel = new PersonalRoom();
        $data = $personalRoomModel->personalRoomFlowingData();
        return json($data);
    }

    /**
     * Notes: 个人房间上周流水数据导出
     * User: 王俊成
     * Date: 2018/11/6
     * Time: 17:53
     */
    public function personalRoomFlowingExport(){
        $personalRoomModel = new PersonalRoom();
        $personalRoomModel->personalRoomFlowingExport();
    }

    /**
     * @return \think\response\View
     * @user 帅帅吴
     * @date 2018/12/24
     * @time 16:12
     * @title 个人房间流水排行榜
     */
    public function PersonalRoomFlowingRank()
    {
        return view();
    }

    /**
     * @return array
     * @user shuaisuhai wu
     * @date 2018/12/24
     * @time 17:24
     * @title 个人房间流水排行榜
     */
    public function PersonalRoomFlowingRankData()
    {
        $giftRecord =  new GiftRecord();
        $data = $giftRecord->PersonalRoomFlowingRankData();
        return $data;
    }

    /**
     * 禁止房间展示
     */
    public function banDisplay()
    {
        return  view();
    }
    //禁止房间展示的数据
    public function banDisplayData()
    {

        $roomModel = new \app\admin\model\Room();
        $data = $roomModel->banDisplayData();
        return json($data);
    }

    public function banDisplayAdd()
    {
        $roomModel = new \app\admin\model\Room();
        $u_id = input('u_id');
        $r_id = User::where('u_number','eq',$u_id)->value('user_id');
        $re = $roomModel->where('user_id','eq',$r_id)->find();
        if($re['room_type'] == 2) return ['code'=>-100,'msg'=>'已经禁止'];
        $res = \app\admin\model\Room::where('user_id','eq',$r_id)->update(['room_type'=> 2]);
        if($res) return ['code'=>100,'msg'=>'禁止成功'];
        else  return ['code'=>-100,'msg'=>'禁止失败'];
    }

    public function banDisplayCancel()
    {
        $room_id = input('id');
        $res = \app\admin\model\Room::where('id','eq',$room_id)->update(['room_type'=> 1]);
        if($res) return ['code'=>100,'msg'=>'取消成功'];
        else  return ['code'=>-100,'msg'=>'取消失败'];
    }

    public function roomClose()
    {
        $room_id = input('room_id');
        $roomMemberModel = new RoomMembers();
        $sortWheatModel = new SortWheat();
        $wyWxModel = new WyYxAPI();
        Db::connect('db_data')->startTrans();
        try{
            $chat_id = RoomModel::where(['id'=>$room_id])->value('chat_id');
            $seatInfo = $roomMemberModel->where(['room_id'=>$room_id])->select();
            foreach($seatInfo as $key => $value){
                if($value['seat'] > 0){
                    $userInfo = User::where(['id'=>$value['user_id']])->find();
                    $userInfo['nickname'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                        return base64_decode($r[1]);
                    }, $userInfo['nickname']);
                    $leave_user = ['nickName'=>$userInfo['nickname'],'type'=>3,'action'=>-1,'account'=>(string)$value['user_id'],'pic' => $userInfo['pic'],'signature' => $userInfo['signature']];
                    $leave_user = json_encode($leave_user);
                    sendAttachMsg($chat_id, $value['user_id'], $leave_user);
                }
                $roomMemberModel->where(['user_id'=>$value['user_id']])->delete();
            }
            $sortWheatModel->where(['room_id'=>$room_id])->delete();
            Db::connect('db_data')->commit();
            return ['code'=>100,'msg'=>'关闭成功'];
        }catch (\Exception $e){
            Db::connect('db_data')->rollback();
        }
    }
}